package com.ym.soso.service.impl;

import com.ym.common.utils.StringUtil;
import com.ym.core.shiro.ShiroUtil;
import com.ym.core.utils.tkmybatis.BaseSVImpl;
import com.ym.medical.enums.CodeRuleEnum;
import com.ym.medical.service.ISysCodeRuleSV;
import com.ym.soso.dao.SveNoseHouseDao;
import com.ym.soso.domain.entity.SveNoseHouseEntity;
import com.ym.soso.domain.param.PublishParam;
import com.ym.soso.domain.param.SveNoseHouseQueryParam;
import com.ym.soso.domain.param.SveNoseHouseSimQueryParam;
import com.ym.soso.domain.param.SveNoseHouseUpdateParam;
import com.ym.soso.domain.vo.SveNoseHouseListPcVO;
import com.ym.soso.domain.vo.SveNoseHouseSimVO;
import com.ym.soso.service.ISveNoseHouseSV;
import com.ym.soso.service.search.service.SveNoseHouseIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 鼻炎馆Service业务层处理
 *
 * @author ym
 * @date 2020-09-02
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SveNoseHouseSVImpl extends BaseSVImpl<SveNoseHouseEntity, SveNoseHouseDao> implements ISveNoseHouseSV {

	@Autowired
	private SveNoseHouseIndexService sveNoseHouseIndexService;

	@Autowired
	private ISysCodeRuleSV sysCodeRuleSV;

	/**
	 * 查询鼻炎馆列表
	 *
	 * @param queryParam 鼻炎馆 查询参数
	 * @return 鼻炎馆
	 */
	@Override
	public List<SveNoseHouseListPcVO> selectListPC(SveNoseHouseQueryParam queryParam) {
		List<SveNoseHouseListPcVO> sveNoseHouses = mapper.selectListPC(queryParam);
		return sveNoseHouses;
	}

	/**
	 * 新增鼻炎馆
	 *
	 * @param updateParam 鼻炎馆 新增参数
	 * @return 结果
	 */
	@Override
	public Long insertPC(SveNoseHouseUpdateParam updateParam) {
		SveNoseHouseEntity entity = updateParam.covert();
		entity.setCreSb(ShiroUtil.getLoginId());
		String code = sysCodeRuleSV.createCode(CodeRuleEnum.RHINITIS_MUSEUM);
		entity.setTheCode(code);
		mapper.insert(entity);
		searchUpdateIndex(entity.getId());
		return entity.getId();
	}

	/**
	 * 修改鼻炎馆
	 *
	 * @param updateParam 鼻炎馆 修改参数
	 * @return 结果
	 */
	@Override
	public int updateByIdPC(SveNoseHouseUpdateParam updateParam) {
		SveNoseHouseEntity entity = updateParam.covert();
		entity.setUptSb(ShiroUtil.getLoginId());
		int rows = this.updateById(entity);
		searchUpdateIndex(entity.getId());
		return rows;
	}

	@Override
	public List<SveNoseHouseSimVO> selectListMobile(SveNoseHouseSimQueryParam queryParam) {
		List<Long> longs = sveNoseHouseIndexService.searchByKeywordOfIds(queryParam);
		if (StringUtil.isNotBlank(queryParam.getKeyword()) && longs.isEmpty()) {
			return Collections.emptyList();
		}
		if (!longs.isEmpty()) {
			queryParam.setIds(longs.toArray(new Long[]{}));
		}
		if (StringUtil.isNotBlank(queryParam.getKeyword())) {
			queryParam.setKeywordSort(true);
		}

		return mapper.selectListMobile(queryParam);
	}

	@Override
	public void release(PublishParam publishParams) {
		this.updateByIds(SveNoseHouseEntity.builder()
						.publisher(ShiroUtil.getLoginId())
						.publishTime(new Date())
						.publishStatus(publishParams.getState())
						.build(),
				publishParams.getIds());
		Long[] ids = publishParams.getIds();
		List<SveNoseHouseEntity> datas = this.selectList(SelectWhere().andIn(SveNoseHouseEntity::getId, ids).end());
		sveNoseHouseIndexService.saveOrUpdate(datas);
	}


	/**
	 * 查询数据并更新
	 */
	private void searchUpdateIndex(Long id) {
		SveNoseHouseEntity entity = this.selectById(id);
		if (entity != null) {
			sveNoseHouseIndexService.saveOrUpdate(entity);
		}
	}
}
